package com.mimc.framework.interceptors;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;

import javax.servlet.http.HttpServletResponse;

public class CrossInterceptor implements Interceptor {
    @Override
    public void intercept(Invocation inv) {
        if(inv.isActionInvocation()){
            Controller c = inv.getController();
            HttpServletResponse response = c.getResponse();
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With,token,lang,source");
            //response.setHeader("Access-Control-Max-Age","2592000");
            if(c.getRequest().getMethod().toLowerCase().equals("options")){
                inv.getController().renderJson(Ret.ok());
                return;
            }else{
                inv.invoke();
            }
        }

    }
}
